
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>redis15 springBoot整合redis · Redis</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        <meta name="author" content="<yangxin19970404@163.com>">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="redis16 RedisUtil.html" />
    
    
    <link rel="prev" href="redis14 Jedis.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="redis01 redis开始.html">
            
                <a href="redis01 redis开始.html">
            
                    
                    redis01 redis开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="redis02 redis安装.html">
            
                <a href="redis02 redis安装.html">
            
                    
                    redis02 redis安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="redis03 redis基础知识.html">
            
                <a href="redis03 redis基础知识.html">
            
                    
                    redis03 redis基础知识
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="redis04 String数据类型详解.html">
            
                <a href="redis04 String数据类型详解.html">
            
                    
                    redis04 String数据类型详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="redis05 List数据类型详解.html">
            
                <a href="redis05 List数据类型详解.html">
            
                    
                    redis05 List数据类型详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="redis06 Set数据类型详解.html">
            
                <a href="redis06 Set数据类型详解.html">
            
                    
                    redis06 Set数据类型详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="redis07 Hash数据类型详解.html">
            
                <a href="redis07 Hash数据类型详解.html">
            
                    
                    redis07 Hash数据类型详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="redis08 Zset数据类型详解.html">
            
                <a href="redis08 Zset数据类型详解.html">
            
                    
                    redis08 Zset数据类型详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="redis09 Geospatial地理位置详解.html">
            
                <a href="redis09 Geospatial地理位置详解.html">
            
                    
                    redis09 Geospatial地理位置详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="redis10 Hyperloglog基数统计详解.html">
            
                <a href="redis10 Hyperloglog基数统计详解.html">
            
                    
                    redis10 Hyperloglog基数统计详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="redis11 Bitmap位图场景详解.html">
            
                <a href="redis11 Bitmap位图场景详解.html">
            
                    
                    redis11 Bitmap位图场景详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="redis12 Redis事务操作.html">
            
                <a href="redis12 Redis事务操作.html">
            
                    
                    redis12 Redis事务操作
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="redis13 Redis实现乐观锁.html">
            
                <a href="redis13 Redis实现乐观锁.html">
            
                    
                    redis13 Redis实现乐观锁
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="redis14 Jedis.html">
            
                <a href="redis14 Jedis.html">
            
                    
                    redis14 Jedis
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.16" data-path="redis15 springBoot整合redis.html">
            
                <a href="redis15 springBoot整合redis.html">
            
                    
                    redis15 springBoot整合redis
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="redis16 RedisUtil.html">
            
                <a href="redis16 RedisUtil.html">
            
                    
                    redis16 RedisUtil
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="redis17 Redis.config详解.html">
            
                <a href="redis17 Redis.config详解.html">
            
                    
                    redis17 Redis.config详解
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="redis18 Redis持久化RDB.html">
            
                <a href="redis18 Redis持久化RDB.html">
            
                    
                    redis18 Redis持久化RDB
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="redis19 Redis持久化AOF.html">
            
                <a href="redis19 Redis持久化AOF.html">
            
                    
                    redis19 Redis持久化AOF
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="redis20 Redis发布订阅.html">
            
                <a href="redis20 Redis发布订阅.html">
            
                    
                    redis20 Redis发布订阅
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="redis21 Redis主动复制.html">
            
                <a href="redis21 Redis主动复制.html">
            
                    
                    redis21 Redis主动复制
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="redis21 Redis哨兵模式.html">
            
                <a href="redis21 Redis哨兵模式.html">
            
                    
                    redis21 Redis哨兵模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="redis22 Redis缓存穿透和雪崩.html">
            
                <a href="redis22 Redis缓存穿透和雪崩.html">
            
                    
                    redis22 Redis缓存穿透和雪崩
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >redis15 springBoot整合redis</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h3 id="springboot&#x6574;&#x5408;redis">SpringBoot&#x6574;&#x5408;Redis</h3>
<ul>
<li>SpringBoot&#x64E6;&#x5076;&#x4F5C;&#x6570;&#x636E;:spring-data jpa jdbc mongodb redis</li>
<li>SpringData&#x4E5F;&#x662F;&#x548C;SpringBoot&#x9F50;&#x540D;&#x7684;&#x9879;&#x76EE;</li>
<li>&#x8BF4;&#x660E;: &#x5728;SpringBoot2.x&#x4E4B;&#x540E;,&#x539F;&#x6765;&#x53EA;&#x7528;&#x7684;Jedis&#x88AB;&#x66FF;&#x6362;&#x4E3A;&#x4E86;lettuce</li>
<li>jedis: &#x91C7;&#x7528;&#x7684;&#x76F4;&#x8FDE;,&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x64CD;&#x4F5C;&#x7684;&#x8BDD;,&#x662F;&#x4E0D;&#x5B89;&#x5168;&#x7684;,&#x5982;&#x679C;&#x60F3;&#x8981;&#x907F;&#x514D;&#x4E0D;&#x5B89;&#x5168;&#x7684;,&#x4F7F;&#x7528;jedis pool&#x8FDE;&#x63A5;&#x6C60;! &#x66F4;&#x50CF; BIO &#x6A21;&#x5F0F;</li>
<li><p>lettuce: &#x91C7;&#x7528;netty,&#x5B9E;&#x4F8B;&#x53EF;&#x4EE5;&#x518D;&#x591A;&#x4E2A;&#x7EBF;&#x7A0B;&#x4E2D;&#x8FDB;&#x884C;&#x5171;&#x4EAB;,&#x4E0D;&#x5B58;&#x5728;&#x7EBF;&#x7A0B;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x60C5;&#x51B5;!&#x53EF;&#x4EE5;&#x51CF;&#x5C11;&#x7EBF;&#x7A0B;&#x6570;&#x636E;&#x4E86;,&#x66F4;&#x50CF; NIO &#x6A21;&#x5F0F;</p>
</li>
<li><p>&#x6E90;&#x7801;:</p>
</li>
</ul>
<pre><code class="lang-java">    <span class="hljs-meta">@Bean</span>
    <span class="hljs-meta">@ConditionalOnMissingBean</span>(name = <span class="hljs-string">&quot;redisTemplate&quot;</span>)<span class="hljs-comment">//&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x4E00;&#x4E2A;redisTemplate&#x6765;&#x66FF;&#x6362;&#x8FD9;&#x4E2A;&#x9ED8;&#x8BA4;&#x7684;</span>
    <span class="hljs-meta">@ConditionalOnSingleCandidate</span>(RedisConnectionFactory.class) <span class="hljs-comment">//&#x81EA;&#x5DF1;&#x5B9A;&#x4E49;&#x914D;&#x7F6E;&#x7C7B;,&#x5C31;&#x4E0D;&#x8D70;&#x8FD9;&#x4E2A;&#x4E86;</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> RedisTemplate&lt;Object, Object&gt; <span class="hljs-title">redisTemplate</span><span class="hljs-params">(RedisConnectionFactory redisConnectionFactory)</span> </span>{
        <span class="hljs-comment">//&#x9ED8;&#x8BA4;&#x7684;RedisTemplate &#x6CA1;&#x6709;&#x8FC7;&#x591A;&#x7684;&#x8BBE;&#x7F6E;,redis&#x5BF9;&#x8C61;&#x90FD;&#x9700;&#x8981;&#x5E8F;&#x5217;&#x5316;</span>
        <span class="hljs-comment">//&#x4E24;&#x4E2A;&#x6CDB;&#x578B;&#x90FD;&#x662F;Object,Object &#x7684;&#x7C7B;&#x578B;,&#x6211;&#x4EEC;&#x540E;&#x4F7F;&#x7528;&#x9700;&#x8981;&#x5F3A;&#x5236;&#x8F6C;&#x6362;&lt;String,Object&gt;</span>
        RedisTemplate&lt;Object, Object&gt; template = <span class="hljs-keyword">new</span> RedisTemplate&lt;&gt;();
        template.setConnectionFactory(redisConnectionFactory);
        <span class="hljs-keyword">return</span> template;
    }

    <span class="hljs-meta">@Bean</span>
    <span class="hljs-meta">@ConditionalOnMissingBean</span> <span class="hljs-comment">// &#x7531;&#x4E8E;String&#x7C7B;&#x578B;&#x662F;redis&#x4E2D;&#x6700;&#x5E38;&#x7528;&#x7684;&#x7C7B;&#x578B;,&#x6240;&#x4EE5;&#x5355;&#x72EC;&#x63D0;&#x51FA;&#x6765;&#x4E00;&#x4E2A;</span>
    <span class="hljs-meta">@ConditionalOnSingleCandidate</span>(RedisConnectionFactory.class)
    <span class="hljs-function"><span class="hljs-keyword">public</span> StringRedisTemplate <span class="hljs-title">stringRedisTemplate</span><span class="hljs-params">(RedisConnectionFactory redisConnectionFactory)</span> </span>{
        StringRedisTemplate template = <span class="hljs-keyword">new</span> StringRedisTemplate();
        template.setConnectionFactory(redisConnectionFactory);
        <span class="hljs-keyword">return</span> template;
    }
</code></pre>
<blockquote>
<p>&#x6574;&#x5408;&#x6D4B;&#x8BD5;&#x4E00;&#x4E0B;</p>
</blockquote>
<ul>
<li>&#x914D;&#x7F6E;&#x6587;&#x4EF6;</li>
</ul>
<pre><code class="lang-properties">spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.lettuce.pool.max-active=8
</code></pre>
<ul>
<li>&#x6D4B;&#x8BD5;</li>
</ul>
<pre><code class="lang-java">        <span class="hljs-comment">//redisTemplate  &#x64CD;&#x4F5C;&#x4E0D;&#x540C;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;,api&#x548C;&#x6211;&#x4EEC;&#x7684;&#x6307;&#x4EE4;&#x662F;&#x4E00;&#x6837;&#x7684;</span>
        <span class="hljs-comment">//opsForValue    &#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;  &#x7C7B;&#x4F3C;,String</span>
        <span class="hljs-comment">//opsForList    &#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;  &#x7C7B;&#x4F3C;,String</span>
        <span class="hljs-comment">//opsForSet    &#x64CD;&#x4F5C;&#x5B57;&#x7B26;&#x4E32;</span>
        <span class="hljs-comment">//opsForHash</span>
        <span class="hljs-comment">//opsForZSet</span>
        <span class="hljs-comment">//opsForGeo</span>
        <span class="hljs-comment">//opsForHyperloglog</span>
        redisTemplate.opsForValue().set(<span class="hljs-string">&quot;key&quot;</span>,<span class="hljs-string">&quot;value&quot;</span>);

        <span class="hljs-comment">//&#x9664;&#x4E86;&#x8FDB;&#x672C;&#x7684;&#x64CD;&#x4F5C;,&#x6211;&#x4EEC;&#x5E38;&#x7528;&#x7684;&#x65B9;&#x6CD5;&#x90FD;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x901A;&#x8FC7;redisTemplate&#x64CD;&#x4F5C;,&#x6BD4;&#x5982;&#x4E8B;&#x52A1;,&#x57FA;&#x672C;&#x7684;CRUD</span>

        <span class="hljs-comment">//&#x83B7;&#x53D6;redis&#x7684;&#x94FE;&#x63A5;&#x5BF9;&#x8C61;</span>
        RedisConnection connection = redisTemplate.getConnectionFactory().getConnection();
        connection.flushDb();
        connection.close();
</code></pre>
<h4 id="&#x81EA;&#x5DF1;&#x7F16;&#x5199;redistemplate">&#x81EA;&#x5DF1;&#x7F16;&#x5199;RedisTemplate</h4>
<pre><code class="lang-java">    <span class="hljs-comment">//&#x7F16;&#x5199;&#x81EA;&#x5DF1;&#x914D;&#x7F6E;&#x7C7B;,&#x56FA;&#x5B9A;&#x7684;,&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x4F7F;&#x7528;</span>
    <span class="hljs-meta">@Bean</span>
    <span class="hljs-meta">@Qualifier</span>(<span class="hljs-string">&quot;redisTemplate&quot;</span>)
    <span class="hljs-meta">@SuppressWarnings</span>(<span class="hljs-string">&quot;all&quot;</span>)
    <span class="hljs-function"><span class="hljs-keyword">public</span> RedisTemplate&lt;String, Object&gt; <span class="hljs-title">redisTemplate</span><span class="hljs-params">(RedisConnectionFactory factory)</span> </span>{
        RedisTemplate&lt;String, Object&gt; template = <span class="hljs-keyword">new</span> RedisTemplate&lt;&gt;();
        <span class="hljs-comment">//&#x8FDE;&#x63A5;&#x5DE5;&#x5382;</span>
        template.setConnectionFactory(factory);
        <span class="hljs-comment">//&#x914D;&#x7F6E;&#x5177;&#x4F53;&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;</span>
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer =
                <span class="hljs-keyword">new</span> Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = <span class="hljs-keyword">new</span> ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);
        <span class="hljs-comment">//String &#x7684;&#x5E8F;&#x5217;&#x5316;</span>
        StringRedisSerializer stringRedisSerializer = <span class="hljs-keyword">new</span> StringRedisSerializer();

        <span class="hljs-comment">//key&#x91C7;&#x7528;String&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;</span>
        template.setKeySerializer(stringRedisSerializer);
        <span class="hljs-comment">//hash&#x7684;key&#x4E5F;&#x91C7;&#x7528;String&#x7684;&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;</span>
        template.setHashKeySerializer(stringRedisSerializer);
        <span class="hljs-comment">//value&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;&#x91C7;&#x7528;jackson</span>
        template.setValueSerializer(jackson2JsonRedisSerializer);
        <span class="hljs-comment">//hash&#x7684;value&#x5E8F;&#x5217;&#x5316;&#x65B9;&#x5F0F;&#x91C7;&#x7528;jackson</span>
        template.setHashValueSerializer(jackson2JsonRedisSerializer);
        template.afterPropertiesSet();
        <span class="hljs-keyword">return</span> template;
    }
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="redis14 Jedis.html" class="navigation navigation-prev " aria-label="Previous page: redis14 Jedis">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="redis16 RedisUtil.html" class="navigation navigation-next " aria-label="Next page: redis16 RedisUtil">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"redis15 springBoot整合redis","level":"1.16","depth":1,"next":{"title":"redis16 RedisUtil","level":"1.17","depth":1,"path":"redis16 RedisUtil.md","ref":"redis16 RedisUtil.md","articles":[]},"previous":{"title":"redis14 Jedis","level":"1.15","depth":1,"path":"redis14 Jedis.md","ref":"redis14 Jedis.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"<yangxin19970404@163.com>","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Redis","language":"zh-hans","gitbook":"3.2.3","description":"Redis > 缓存技术"},"file":{"path":"redis15 springBoot整合redis.md","mtime":"2021-03-19T11:39:39.366Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2021-04-17T07:48:08.480Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

